package windows;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;

import bussinesmodel.CAdministrator;

import utilities.CListModel;
import dataTables.CDataTableIngredients;
import dataTables.CDataTableSupplier;
import dbblink.CQuery;


public class CAddIngredientPane extends JPanel
{
	
	
	    private javax.swing.JButton botonAdd;
	    private javax.swing.JButton buttonSave;
	    private javax.swing.JButton buttonSearchIngredient;
	    private javax.swing.JTextField fieldIngredient;
	    private javax.swing.JLabel labeIngredient;
	    private javax.swing.JLabel labelSuppliers;
	    private javax.swing.JLabel labelTittle;
	    private javax.swing.JList listADDEDSuppliers;
	    private javax.swing.JList listADDSuppliers;
	    private javax.swing.JScrollPane scrollListADDEDSuppliers;
	    private javax.swing.JScrollPane scrollListADDSuppliers;
	    private javax.swing.JSeparator separadorTitulo;

	    private CAdministrator administrator;
	    
	    private CQuery query;
	    String[] saListADD;
	    Object a[];
	    
	    public CAddIngredientPane() 
	    {
	        initComponents();
	    }
	    
	    
	    private void initComponents() {

	        labelTittle = new javax.swing.JLabel();
	        separadorTitulo = new javax.swing.JSeparator();
	        labeIngredient = new javax.swing.JLabel();
	        fieldIngredient = new javax.swing.JTextField();
	        labelSuppliers = new javax.swing.JLabel();
	        scrollListADDSuppliers = new javax.swing.JScrollPane();
	        listADDSuppliers = new javax.swing.JList();
	        botonAdd = new javax.swing.JButton();
	        scrollListADDEDSuppliers = new javax.swing.JScrollPane();
	        listADDEDSuppliers = new javax.swing.JList();
	        buttonSave = new javax.swing.JButton();
	        buttonSearchIngredient = new javax.swing.JButton();

	        
	        setLayout(null);

	        labelTittle.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
	        labelTittle.setForeground(new java.awt.Color(0, 0, 102));
	        labelTittle.setText("Ingrese el nuevo Ingrediente");
	        add(labelTittle);
	        labelTittle.setBounds(50, 30, 210, 17);

	        separadorTitulo.setForeground(new java.awt.Color(0, 0, 0));
	        add(separadorTitulo);
	        separadorTitulo.setBounds(40, 52, 220, 2);

	        labeIngredient.setText("Nombre Ingrediente:");
	        add(labeIngredient);
	        labeIngredient.setBounds(40, 90, 130, 14);

	        fieldIngredient.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                
	            }
	        });
	        add(fieldIngredient);
	        fieldIngredient.setBounds(180, 90, 180, 20);

	        listADDSuppliers.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
	        
	        labelSuppliers.setText("Proveedores:");
	        add(labelSuppliers);
	        labelSuppliers.setBounds(40, 140, 80, 14);

	   
	        scrollListADDSuppliers.setViewportView(listADDSuppliers);

	        add(scrollListADDSuppliers);
	        scrollListADDSuppliers.setBounds(180, 140, 100, 70);

	        botonAdd.setText(">>>");
	        botonAdd.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) 
	            {
	            	   a = listADDSuppliers.getSelectedValues();	
	   	               listADDEDSuppliers.setListData(listADDSuppliers.getSelectedValues());
	                
	            }
	        });
	        add(botonAdd);
	        botonAdd.setBounds(290, 150, 60, 23);

	      
	    
	        scrollListADDEDSuppliers.setViewportView(listADDEDSuppliers);

	        add(scrollListADDEDSuppliers);
	        scrollListADDEDSuppliers.setBounds(360, 140, 100, 70);

	       

	        buttonSave.setText("Guardar");
	        buttonSave.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) 
	            {	               	        	            	
	            	try
	            	{
	            	  administrator.saveIngredient(fieldIngredient.getText(),a);
	            	}
	            	catch(Exception e)
	            	{
	            		JOptionPane.showMessageDialog(null, "Agregue el o los Proveedores","Error",JOptionPane.ERROR_MESSAGE);
	            	}
	            	
	            }
	        });
	        
	        
	        
	        fieldIngredient.addKeyListener(new KeyAdapter() {
				public void keyTyped(KeyEvent e) {
					char c = e.getKeyChar();
					if ((Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE))) {
					} else {
						String texto = String.valueOf(c);
						e.consume();
						fieldIngredient.setText(fieldIngredient.getText()
								+ texto.toUpperCase());
					}
				}
			});
	        
	        
	        add(buttonSave);
	        buttonSave.setBounds(320, 240, 85, 23);

	        buttonSearchIngredient.setText("Buscar Ingrediente");
	        buttonSearchIngredient.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) 
	            {
	               
	            	administrator.showIngredientSearchPane();
	            	
	            }
	        });
	        add(buttonSearchIngredient);
	        buttonSearchIngredient.setBounds(170, 240, 140, 23);

	        fillList();
	        
	        setLocation(230, 60);
			setSize(400, 500);
			
			
	    }


	    public void fillList()
		{
			
			query=new CQuery();
			String sQuery="Select * From supplier";
			ArrayList<CDataTableSupplier> aSuppliers;
			aSuppliers=query.showSuppliers(sQuery);
			saListADD = new String[aSuppliers.size()];
			
			CDataTableSupplier aux;
			
			
			for(int i=0;i<saListADD.length;i++)
			{
				aux=aSuppliers.get(i);
				saListADD[i]=aux.getSSupplierName();
			}
		    
			listADDSuppliers.setModel(new CListModel(saListADD)); 
			

		}
	    
	    
	    
		public void cleanFields()
		{
			fieldIngredient.setText("");
			String sanewListADD[]=new String[1];
			listADDEDSuppliers.setModel(new CListModel(sanewListADD)); 
			
		}
	    
	    
	    
		public CAdministrator getAdministrator() {
			return administrator;
		}


		public void setAdministrator(CAdministrator administrator) {
			this.administrator = administrator;
		}


		public javax.swing.JButton getBotonAdd() {
			return botonAdd;
		}


		public javax.swing.JButton getButtonSave() {
			return buttonSave;
		}


		public javax.swing.JButton getButtonSearchIngredient() {
			return buttonSearchIngredient;
		}


		public javax.swing.JTextField getFieldIngredient() {
			return fieldIngredient;
		}


		public javax.swing.JLabel getLabeIngredient() {
			return labeIngredient;
		}


		public javax.swing.JLabel getLabelSuppliers() {
			return labelSuppliers;
		}


		public javax.swing.JLabel getLabelTittle() {
			return labelTittle;
		}


		public javax.swing.JList getListADDEDSuppliers() {
			return listADDEDSuppliers;
		}


		public javax.swing.JList getListAddSuppliers() {
			return listADDSuppliers;
		}


		public javax.swing.JScrollPane getScrollListADDEDSuppliers() {
			return scrollListADDEDSuppliers;
		}


		public javax.swing.JScrollPane getScrollListADDSuppliers() {
			return scrollListADDSuppliers;
		}


		public javax.swing.JSeparator getSeparadorTitulo() {
			return separadorTitulo;
		}
}
